package org.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController //ajax
public class TestRedisController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/keys")
    public Set<String> keys() {
        return redisTemplate.keys("*");
    }

    @GetMapping("/delete")
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    @GetMapping("/clear")
    public void clear() {
        redisTemplate.delete(redisTemplate.keys("*"));
    }

    @RequestMapping("/set")
    public void addStr(String key, String value) {
        //.opsForXXX 选择操作的类型
        redisTemplate.opsForValue().set(key, value);
    }

    @RequestMapping("/get")
    public String getStr(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @RequestMapping("/rpush")
    public void rpush(String key, String value) {
        redisTemplate.opsForList().rightPush(key, value);
    }

    @RequestMapping("/range")
    public List<String> range(String key) {
        //长度
        long len = redisTemplate.opsForList().size(key);
        //获取从0到长度
        return redisTemplate.opsForList().range(key, 0, len);
    }

    public void testSet(String key) {
//        redisTemplate.opsForSet().add();
        redisTemplate.opsForSet().size(key);
        Set<String> members = redisTemplate.opsForSet().members(key);
//        redisTemplate.opsForSet().remove()

//        redisTemplate.opsForZSet().add()
//        redisTemplate.opsForZSet().size()
//        redisTemplate.opsForZSet().range()
        //从低到高
//        redisTemplate.opsForZSet().rangeByScore()
        //从高到低
//        redisTemplate.opsForZSet().reverseRangeByScore()
    }

    @RequestMapping("/put")
    public void put(String key, String hashKey, String value) {

        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    @RequestMapping("/hashKeys")
    public Set<Object> hashKeys(String key) {

        return redisTemplate.opsForHash().keys(key);
    }

    @RequestMapping("/hashValues")
    public List<Object> values(String key) {

        return redisTemplate.opsForHash().values(key);
    }
}
